package com.huaxindata.im.main.contactview.friend;

import android.Manifest;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.huaxin.common.view.avatarimageview.AvatarImageView;
import com.huaxin.common.view.avatarimageview.HxAvatarImageGlide;
import com.huaxindata.im.R;
import com.huaxindata.im.constants.ImConstants;
import com.huaxindata.im.main.addFriend.AddFriendActivity;
import com.huaxindata.im.main.chat.ConversationActivity;
import com.huaxindata.im.main.chat.ConversationManager;
import com.huaxindata.im.main.manager.ContactManager;
import com.huaxindata.im.model.contact.Contact;
import com.huaxindata.im.model.contact.ContactLocal;
import com.huaxindata.im.model.contact.ContactPerson;

/**
 * 用于显示联系人资料信息的圣话框
 */
public class ContactInfoDialog extends Dialog {

    private AvatarImageView mIconIv;
    private TextView        mIconExpandTv;
    private TextView        mNameTv;

    private TextView mCompanyTv;
    private TextView mDutyTv;
    private TextView mPhoneTv;

    private TextView  mChatTv;//发消息或发短信
    private TextView  mDialTv;//打电话
    private ImageView mCloseIv;
    private Contact   mContact;

    public ContactInfoDialog(Context context) {
        super(context, R.style.Simple_Dialog);
    }

    private void initView() {
        mIconIv = (AvatarImageView) findViewById(R.id.dialog_contact_icon_iv);
        mIconExpandTv = (TextView) findViewById(R.id.dialog_contact_icon_tv);
        mNameTv = (TextView) findViewById(R.id.dialog_contact_name_tv);
        mCompanyTv = (TextView) findViewById(R.id.dialog_contact_company_tv);
        mDutyTv = (TextView) findViewById(R.id.contact_dialog_duty_tv);
        mPhoneTv = (TextView) findViewById(R.id.contact_dialog_phone_tv);
        mChatTv = (TextView) findViewById(R.id.contact_dialog_chat_tv);
        mDialTv = (TextView) findViewById(R.id.contact_dialog_dial_tv);
        mCloseIv = (ImageView) findViewById(R.id.dialog_contact_close_iv);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_concat_info);
        setCanceledOnTouchOutside(true);
        initView();
        mCloseIv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        mDialTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                call();
                dismiss();
            }
        });
        mChatTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                chat();
                dismiss();
            }
        });
        Window window = getWindow();
        window.getDecorView().setPadding(0, 0, 0, 0);
        WindowManager.LayoutParams lp = window.getAttributes();
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(lp);

        window.setGravity(Gravity.BOTTOM); // 此处可以设置dialog显示的位置
        window.setWindowAnimations(R.style.DialogBottom); // 添加动画

        if (mContact != null) {
            mNameTv.setText(mContact.getNickName());
            mCompanyTv.setText(mContact.getCompany());

            Drawable top = getContext().getResources().getDrawable(
                    TextUtils.isEmpty(mContact.getPhone()) ? R.mipmap.concat_info5 : R.mipmap.concat_info2);
            top.setBounds(0, 0, top.getIntrinsicWidth(), top.getIntrinsicHeight());
            final int color = TextUtils.isEmpty(mContact.getPhone()) ? 0xff888888 : 0xff00a0e9;
            mPhoneTv.setTextColor(color);
            mDialTv.setTextColor(color);
            mDialTv.setCompoundDrawables(null, top, null, null);


            if (mContact instanceof ContactLocal) {
                Drawable top2 = getContext().getResources().getDrawable(
                        TextUtils.isEmpty(mContact.getNickName()) ? R.mipmap.yaoqing_disable : R.mipmap.yaoqing_enable);
                top2.setBounds(0, 0, top2.getIntrinsicWidth(), top2.getIntrinsicHeight());
                mChatTv.setCompoundDrawables(null, top2, null, null);
                //未注册
                if (TextUtils.isEmpty(mContact.getNickName())) {
                    mChatTv.setTextColor(0xff888888);
                    mChatTv.setText("邀请");//暂不可用
                    mDutyTv.setText("游客");
                    mChatTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Toast.makeText(getContext(), "该功能暂不支持", Toast.LENGTH_SHORT).show();
                        }
                    });
                } else {
                    mDutyTv.setText("游客");
                }
            } else if (mContact instanceof ContactPerson) {
                if (!ContactManager.isFriend(mContact.getClientId())) {
                    //非好友
                    Drawable top2 = getContext().getResources().getDrawable(R.mipmap.friend_1);
                    top2.setBounds(0, 0, top2.getIntrinsicWidth(), top2.getIntrinsicHeight());
                    mChatTv.setCompoundDrawables(null, top2, null, null);
                    mChatTv.setText("加为好友");
                    mChatTv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            dismiss();
                            //添加好友界面
                            Intent intent = new Intent(getContext(), AddFriendActivity.class);
                            intent.putExtra(ImConstants.FUNCTION_ADD_FRIEND, ImConstants.FUNCTION_REQUEST);
                            intent.putExtra(ImConstants.CONTACT, (ContactPerson) mContact);
                            getContext().startActivity(intent);
                        }
                    });
                }
                mDutyTv.setText(mContact.getDuty());
            } else {
                mDutyTv.setText(mContact.getDuty());
            }
            if (TextUtils.isEmpty(mContact.getPhone())) {
                mPhoneTv.setText("暂无电话");
                mDialTv.setEnabled(false);
            } else {
                mPhoneTv.setText(mContact.getPhone());
                mDialTv.setEnabled(true);
            }
            // mContact.showImageIcon(getContext(), mIconIv, mIconExpandTv);
            //设置头像
            HxAvatarImageGlide instance = HxAvatarImageGlide.getInstance();
            instance.setHeadImage(getContext(), mContact.getIcon(), mContact.getNickName(), mIconIv);
        }
    }

    public void show(Contact chat) {
        this.mContact = chat;
        show();
    }

    //打开聊天消息界面或者短信界面
    public void chat() {
        if (mContact == null)
            return;
        if (mContact instanceof ContactPerson) {
            Intent intent = new Intent(getContext(), ConversationActivity.class);
            ConversationManager.setChatObject(mContact);
            getContext().startActivity(intent);
        }

        //如果是本地联系人就打开短信界面
        if (mContact instanceof ContactLocal) {
            Intent intent = new Intent(Intent.ACTION_SENDTO, Uri.parse("smsto:" + mContact.getPhone()));
            getContext().startActivity(intent);
        }
    }

    //调用系统打电话功能
    public void call() {
        if (mContact != null) {
            if (ActivityCompat.checkSelfPermission(getContext(), Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                Toast.makeText(getContext(), "需授权", Toast.LENGTH_SHORT).show();
                return;
            }
            Intent intentPhone = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:" + mContact.getPhone()));
            getContext().startActivity(intentPhone);
        }
    }
}
